﻿using CubeCube.Utils;
using CubeCube.World;

namespace CubeCube.Sector
{
    public class Sector
    {
        public IndexPosition sectorPosition;
        public CubeWorld world;
        public IndexPosition tileOffset;
        public ISectorGraphics sectorGraphics;

        public bool insideInvalidateSectorQueue;
        public bool insideInvalidateLightQueue;

        public override int GetHashCode()
        {
            return sectorPosition.GetHashCode();
        }

        public Sector(CubeWorld world, IndexPosition sectorPosition, IndexPosition tileOffset)
        {
            this.world = world;
            this.tileOffset = tileOffset;
            this.sectorPosition = sectorPosition;
        }

        public void SetSectorGraphics(ISectorGraphics graphics)
        {
            if (this.sectorGraphics != null)
                this.sectorGraphics.SetSector(null);

            this.sectorGraphics = graphics;

            if (this.sectorGraphics != null)
                this.sectorGraphics.SetSector(this);
        }

        public ISectorGraphics GetSectorGraphics()
        {
            return this.sectorGraphics;
        }

        public void UpdateMesh()
        {
            if (sectorGraphics != null)
                sectorGraphics.UpdateMesh();
        }

        public void UpdateAmbientLight()
        {
            if (sectorGraphics != null)
                sectorGraphics.UpdateAmbientLight();
        }

        public void Clear()
        {
            world = null;
            sectorGraphics = null;
        }
    }
}
